package com.i2silly.wxbot.utils;

import com.alibaba.fastjson.JSON;
import com.i2silly.wxbot.constant.WxReplyConstant;
import com.i2silly.wxbot.constant.WxRobotConstant;
import com.i2silly.wxbot.pojo.WsReceive;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @program: wx-bot
 * @description：回复工具类
 * @author: pzhongqiang@linewell.com
 * @date: 2020年11月28日 21:47:38
 */
public class ReplyUtil {

    /**
     * 获取统计信息
     *
     * @return
     */
    public static String getStatics(int personNum) {
        return WxReplyConstant.STATISTICS + WxReplyConstant.STATISTICS_TIME + PubUtils.getCurrentTime() + "\n" + WxReplyConstant.PERSON_NUM + personNum;
    }

    /**
     * 没法接的时候随机回复
     */
    public static String getError() {
        String[] error = {"抱歉，这句话我接不了。", "你说啥呢，我听不懂~", "等我学一下再来回答你。"};
        return error[getRandom(error.length)];
    }

    /**
     * 当前时间
     */
    public static String getCurTime() {
        return WxReplyConstant.TIME + WxReplyConstant.CUR_TIME + PubUtils.getCurrentTime();
    }

    public static int getRandom(int num) {
        Random random = new Random();
        return random.nextInt(num);
    }

    public static String getFunction() {
        String s = PubUtils.doGet(WxRobotConstant.PROTOCOL + WxRobotConstant.LOCALHOST + WxRobotConstant.COLON + WxRobotConstant.LOCALHOST_PORT + WxRobotConstant.FUNCTION_JSON, null);
        List<Map> maps = JSON.parseArray(s, Map.class);
        StringBuilder sb = new StringBuilder();
        sb.append(WxReplyConstant.FUNCTION);
        for (Map map : maps) {
            sb.append(map.get("index")).append(". ").append(map.get("name")).append("\n");
        }
        sb.append("******************************\n").append("请输入对应数字选择指令");
        return sb.toString();
    }

    public static boolean isSend(String sendWxId, String content,String wake, WsReceive lastReceive) {
        boolean flag = false;
        if (lastReceive == null || lastReceive.getCommand() < 0) {
            if (!"self".equals(sendWxId) && (content.contains("@this") || content.contains(wake))) {
                flag = true;
            }
        } else {
            flag = true;
        }
        return flag;
    }

    public static Map<String, Object> smartChat(String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("app_id", WxRobotConstant.APP_ID);
        map.put("time_stamp", System.currentTimeMillis() / 1000);
        map.put("nonce_str", PubUtils.random(10));
        map.put("session", "10000");
        map.put("question", content);
        String sign = getSign(map);
        map.put("sign", sign);
        return map;
    }

    public static String getSign(Map<String, Object> map) {
        Set<String> set = map.keySet();
        set = new TreeSet<>(set);
        StringBuilder sb = new StringBuilder();
        for (String s : set) {
            try {
                sb.append(s).append("=").append(URLEncoder.encode(String.valueOf(map.get(s)),"UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        sb.append("app_key=").append(WxRobotConstant.APP_KEY);
        return PubUtils.encryptMD5(sb.toString()).toUpperCase();
    }

    public static void main(String[] args) {
//        System.out.println(PubUtils.encryptMD5("123456"));
//        System.out.println(URLEncoder.encode(" "));
//        System.out.println(URLDecoder.decode("%E8"));
        /*for (int i = 0; i < 10; i++) {
            String random = PubUtils.random(6);
            System.out.println(random);
        }*/
    }
}
